{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#数据准备\n",
    "import numpy as np\n",
    "import matplotlib.pyplot as plt\n",
    "x=np.random.uniform(-3,3,size=100)\n",
    "X=x.reshape(-1,1)\n",
    "y=0.5*x**2+x+2+np.random.normal(0,1,100)\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "train_test_split的意义"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "from sklearn.model_selection import train_test_split\n",
    "x_train,x_test,y_train,y_test=train_test_split(X,y,test_size=0.2)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2.4180779662461154"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#线性回归\n",
    "from sklearn.metrics import mean_squared_error\n",
    "from sklearn.linear_model import LinearRegression\n",
    "lin_reg=LinearRegression()\n",
    "lin_reg.fit(x_train,y_train)\n",
    "y_predict=lin_reg.predict(x_test)\n",
    "mean_squared_error(y_test,y_predict)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.2368331415333986"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#多项式回归，项数为2\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.pipeline import Pipeline\n",
    "def PolynormialRegression(degree):\n",
    "    return Pipeline([\n",
    "        (\"poly\",PolynomialFeatures(degree)),\n",
    "        (\"std\",StandardScaler()),\n",
    "        (\"lin_reg\",LinearRegression())\n",
    "    ])\n",
    "\n",
    "poly_reg=PolynormialRegression(degree=2)\n",
    "poly_reg.fit(x_train,y_train)\n",
    "y2_predict=poly_reg.predict(x_test)\n",
    "mean_squared_error(y_test,y2_predict)#泛化能力对比线性回归变好了"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1.5789178196804006"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "#多项式，调节项为10\n",
    "#多项式回归，项数为2\n",
    "from sklearn.preprocessing import PolynomialFeatures\n",
    "from sklearn.preprocessing import StandardScaler\n",
    "from sklearn.pipeline import Pipeline\n",
    "def PolynormialRegression(degree):\n",
    "    return Pipeline([\n",
    "        (\"poly\",PolynomialFeatures(degree)),\n",
    "        (\"std\",StandardScaler()),\n",
    "        (\"lin_reg\",LinearRegression())\n",
    "    ])\n",
    "\n",
    "poly_reg=PolynormialRegression(degree=10)\n",
    "poly_reg.fit(x_train,y_train)\n",
    "y2_predict=poly_reg.predict(x_test)\n",
    "mean_squared_error(y_test,y2_predict)#泛化能力对比最高幂次为2，变差了！"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.9.7"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
